<?php
	header('Content-Type:text/html; charset=utf-8');
	$iipp=$_SERVER["REMOTE_ADDR"];
	if($iipp=="42.121.91.11")
	{
		$iipp=$_SERVER["HTTP_X_FORWARDED_FOR"];
		if(!filter_var($iipp,FILTER_VALIDATE_IP))
		{
			echo "非法操作，请返回";
		}
	}

	if(isset($_POST['checked'])){
		include 'config.sql.php';
		if(mysql_num_rows(mysql_query("SELECT * FROM voter WHERE ip = '$iipp'",$link)) > 0)
			echo "<script>alert('您已经投过票了，请不要重复投票。');location='index.php';</script>";
		else
		{
			$selectid=array();
			$selectid=$_POST['checked'];
			foreach ($selectid as $id)
			{
				$result = mysql_query("SELECT * FROM votes WHERE _id = '$id'",$link); //投票id的行
				$idd = mysql_result($result, 0, "vote") + 1;  //投票数+1
				$sqlupdate =  "UPDATE `ustb`.`votes` SET `vote` = '".$idd."' WHERE `votes`.`_id` =".$id;     //更新记录
				mysql_query($sqlupdate,$link);
				$sqlupdate =  "INSERT INTO `ustb`.`voter` (`id`, `ip`) VALUES ($id, '".$iipp."')";//更新ip记录 ===========================无法获取username 
				mysql_query($sqlupdate,$link);	
			}
			echo "<script>alert('感谢您的参与！学生们的肯定，是老师心中最大的温暖！您的心意，我们一定帮您传达到！祝好！');location='index.php';</script>";
			//echo "<script>alert('投票成功！');location='index.php?p=result';</script>";	
		}		
	mysql_close();
	}

?>
